package com.dufcal.repondeursms;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class Parametres extends Activity {

	private CheckBox check = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parametres);

		RelativeLayout view = (RelativeLayout) findViewById(R.id.ParametreLayout);
		view.setBackgroundColor(Color.argb(135, 154, 102, 185));
		view.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				v.requestFocusFromTouch();
				return false;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(Appli.appli);

		EditText editText = (EditText) findViewById(R.id.editTextMessageIndicatif);
		editText.setText(preferences.getString("indicatif",
				this.getString(R.string.INDICATIFPAYS)));

		CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
		check.setChecked(preferences.getBoolean("international", false));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_parametres, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == R.id.menu_settings) {
			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(Appli.appli);
			Editor edit = preferences.edit();
			edit.putBoolean("international", false);
			edit.putBoolean("pasRepeter", false);
			edit.putString("indicatif", this.getString(R.string.INDICATIFPAYS));
			edit.commit();
			onResume();
		} else if (id == R.id.menuParametre) {
			String message = this.getString(R.string.aideParametre);
			String okBoutton = this.getString(R.string.continuer);
			Dialogue.afficherDialogue(message, okBoutton, this);
		}

		/*
		 * if (id == R.id.credits) { Toast.makeText(this,
		 * Appli.appli.getString(R.string.credit), Toast.LENGTH_SHORT).show(); }
		 */

		return super.onOptionsItemSelected(item);
	}

	public void onInternational(View v) {

		check = (CheckBox) findViewById(R.id.checkBox1);
		boolean isCheck = check.isChecked();

		if (isCheck) {

			AlertDialog.Builder builder = new AlertDialog.Builder(this);

			// Ajoute un texte
			builder.setMessage(this.getString(R.string.attention));

			// Ajoute un boutton
			builder.setPositiveButton(Appli.appli.getString(R.string.accepter),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							// User clicked OK button
							SharedPreferences preferences = PreferenceManager
									.getDefaultSharedPreferences(Appli.appli);
							Editor edit = preferences.edit();
							edit.putBoolean("international", true);
							edit.commit();
						}
					});
			// Ajoute un autre boutton
			builder.setNegativeButton(Appli.appli.getString(R.string.annuler),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							// User cancelled the dialog
							check.setChecked(false);
						}
					});

			// Create the AlertDialog
			AlertDialog dialog = builder.create();
			dialog.show();

		} else {

			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(Appli.appli);
			Editor edit = preferences.edit();
			edit.putBoolean("international", false);
			edit.commit();
		}

	}

	public void onRegle(View v) {
		Intent intent = new Intent(this, RegleActivity.class);
		startActivity(intent);

	}

	public void onChangement(View v) {
		Intent intent = new Intent(this, ChangementMessage.class);
		startActivity(intent);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		EditText editText = (EditText) findViewById(R.id.editTextMessageIndicatif);

		if (editText.getEditableText().toString().length() > 0
				&& editText.getEditableText().toString().substring(0, 1)
						.equals("+")) {

			SharedPreferences preferences = PreferenceManager
					.getDefaultSharedPreferences(Appli.appli);
			Editor edit = preferences.edit();
			edit.putString("indicatif", editText.getEditableText().toString());
			edit.commit();
		}

	}

}
